package com.runa.rccprotocol.analysis.rn;

import com.runa.monitor.comm.dto.Node;
import com.runa.monitor.comm.dto.newComm.ValveVO;
import com.runa.persistence.task.RequestPendingTask;
import com.runa.protocol.enums.ByteOrder;
import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.analysis.IInterDataAnalysis;
import com.runa.rccprotocol.dto.runa.RunaConcenAffirmDeny;
import com.runa.rccprotocol.dto.runa.RunaConcenResponseData;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Date;

/**
 * 2025/5/20 15:51
 * zhangYB
 */
@Slf4j
public class RunaBuildValveAnalysis extends IInterDataAnalysis {

    public void handleModbusData(String netSerial, byte[] bs, int begin, int length) {
        RequestPendingTask task = RequestPendingTask.getTask(netSerial);
        if (null == task) {
            return;
        }
        Node node = task.getNode();
        if (null == node) {
            return;
        }
        for (int i = begin; i < bs.length; i++) {
            if ((bs[i] & 0xff) == node.getIndex()) {
                if (bs[i+1] == 0x03) {
                    log.debug("瑞纳阀modbus解析: {} {}", i, ByteUtil.byteToHexString(bs, i, length - i));
                    ValveVO valveVO = handReadResponse(node.getSerial(), bs, i);
                    RunaConcenResponseData runaConcenResponseData = new RunaConcenResponseData();
                    runaConcenResponseData.addRepData(valveVO);
                    this.interData = runaConcenResponseData;
                    break;
                } else if (bs[i+1] == 0x10) {
                    this.interData = new RunaConcenAffirmDeny(true);
                    break;
                }
            }
        }

    }

    public ValveVO handReadResponse(String serial, byte[] bs, int begin) {
        ValveVO valveVO = new ValveVO();
        valveVO.setValveSerial(serial);
        valveVO.setReadTime(new Date());
        begin = begin + 3;
        valveVO.setRemoteSet(Float.valueOf(ByteUtil.byteToShort(bs, begin + 1 * 2, ByteOrder.AB)));
        valveVO.setActOpen(Float.valueOf(ByteUtil.byteToShort(bs, begin + 3 * 2, ByteOrder.AB)));
        valveVO.setSoftVersion(String.valueOf(ByteUtil.byteToShort(bs, begin + 8 * 2, ByteOrder.AB)));
        valveVO.setSupplyTemp(ByteUtil.byteToFloat(bs, begin + 10 * 2, ByteOrder.CDAB));
        valveVO.setReturnTemp(ByteUtil.byteToFloat(bs, begin + 12 * 2, ByteOrder.CDAB));
        valveVO.setEnvTemp(ByteUtil.byteToFloat(bs, begin + 14 * 2, ByteOrder.CDAB));
        valveVO.setSupplyPre(ByteUtil.byteToFloat(bs, begin + 16 * 2, ByteOrder.CDAB));
        valveVO.setBehindPre(ByteUtil.byteToFloat(bs, begin + 18 * 2, ByteOrder.CDAB));
        valveVO.setFrontPre(ByteUtil.byteToFloat(bs, begin + 20 * 2, ByteOrder.CDAB));
        valveVO.setFlow(ByteUtil.byteToFloat(bs, begin + 22 * 2, ByteOrder.CDAB));
        String status = ByteUtil.byteToHexString(bs, begin + 32 * 2, 2);
        valveVO.setStatus(status);
        ArrayList<String> list = new ArrayList<>();
        int sta = Integer.parseInt(status, 16);
        if ((sta >> 4 & 1) == 1) {
            list.add("楼阀堵转");
        }
        if ((sta >> 12 & 1) == 1) {
            list.add("阀体分离");
        }
        valveVO.setFaultCode(list);
        Short lr = ByteUtil.byteToShort(bs, begin + 33 * 2, ByteOrder.AB);
        valveVO.setLR(lr == 0 ? ValveVO.LREnum.本地 : ValveVO.LREnum.远程);
        return valveVO;
    }

    public static void main(String[] args) {
        String s = "CC CD 42 C7";
        byte[] bytes = ByteUtil.hexStringToByte(s);
        Float aFloat = ByteUtil.byteToFloat(bytes, ByteOrder.CDAB);
        System.out.println(aFloat);
    }


}
